#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

class Solution 
{
public:
    int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) 
    {
        int length = nums1.size();
        unordered_map<int, int> sum1;
        unordered_map<int, int> sum2;
        int count = 0;

        for(int i = 0; i < length; i++)
        {
            for(int j = 0; j < length; j++)
            {
                int temp1 = nums1[i] + nums2[j];
                auto it = sum1.find(temp1);
                if(it == sum1.end())
                {
                    sum1[temp1] = 1;
                }
                else
                {
                    it->second ++;
                }

                int temp2 = nums3[i] + nums4[j];
                auto it2 = sum2.find(temp2);
                if(it2 == sum2.end())
                {
                    sum2[temp2] = 1;
                }
                else
                {
                    it2->second ++;
                }
            }
        }
        for(auto it = sum1.begin(); it != sum1.end(); it++)
        {
            int target = 0 - it->first;
            auto it2 = sum2.find(target);
            if(it2 != sum2.end())
            {
                count += (it->second * it2->second);
            }
        }
        return count;
    }
};

int main()
{
    Solution sol;

    // 测试用例1
    vector<int> nums1_1 = {1, 2};
    vector<int> nums2_1 = {-2, -1};
    vector<int> nums3_1 = {-1, 2};
    vector<int> nums4_1 = {0, 2};
    cout << "测试用例1结果: " << sol.fourSumCount(nums1_1, nums2_1, nums3_1, nums4_1) << endl; // 2

    // 测试用例2
    vector<int> nums1_2 = {0};
    vector<int> nums2_2 = {0};
    vector<int> nums3_2 = {0};
    vector<int> nums4_2 = {0};
    cout << "测试用例2结果: " << sol.fourSumCount(nums1_2, nums2_2, nums3_2, nums4_2) << endl; // 1

    // 测试用例3
    vector<int> nums1_3 = {1, 2, -1};
    vector<int> nums2_3 = {-2, -1, 2};
    vector<int> nums3_3 = {-1, 2, 0};
    vector<int> nums4_3 = {0, 2, -2};
    cout << "测试用例3结果: " << sol.fourSumCount(nums1_3, nums2_3, nums3_3, nums4_3) << endl;

    // 测试用例4
    vector<int> nums1_4 = {1};
    vector<int> nums2_4 = {1};
    vector<int> nums3_4 = {-1};
    vector<int> nums4_4 = {-1};
    cout << "测试用例4结果: " << sol.fourSumCount(nums1_4, nums2_4, nums3_4, nums4_4) << endl; // 1

    return 0;
}
